package com.yn.courseselectionsystem.config;

import com.yn.courseselectionsystem.pojo.MyAddress;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

import javax.jms.Queue;
import javax.jms.Topic;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @description: TODO
 * @author: Tokyo_God
 * @date: 2020/12/28 8:36
 * Role of:
 * @version: v1.0
 */
@SpringBootConfiguration
public class ActiveConfig {

    @Bean(name = "myAddress")
    public MyAddress myAddress(){
        InetAddress address= null;
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        String path="127.0.0.1";
        if(address!=null){
            path=address.getHostAddress();
        }
        /*session.setAttribute("myAdress",path);*/
        System.out.println("初始化获取的IP地址"+path);
        /////////////////////////////获取 地址有问题  不是自己的地址////////////////////////////
        MyAddress myAdress=new MyAddress();
        myAdress.setAddress(path);
        return myAdress;
    }

    @Value("${registerTopic}")
    private String registerTopic;
    @Value("${queueName}")
    private String queueName;

    @Value("${spring.activemq.user}")
    private String userName;
    @Value("${spring.activemq.password}")
    private String password;
    @Value("${spring.activemq.broker-url}")
    private String  brokerUrl;


    /**
     * 返回一个目的地 参数是队列名称
     * @return
     */
    @Bean
    public Queue queue(){
        /*System.out.println("111::"+userName);*/
        return new ActiveMQQueue(queueName);
    }


    /**
     * 返回一个目的地  参数是主题（）名称
     * @return
     */
    @Bean
    public Topic topic(){
        return new ActiveMQTopic(registerTopic);
    }

    /**
     * 返回一个创造连接的工厂  参数是消息中间件的用户名  密码  和"tcp://127.0.0.1:61616"
     * @return
     */
    @Bean
    public ActiveMQConnectionFactory connectionFactory(){
        return new ActiveMQConnectionFactory(userName,password,brokerUrl);
    }

    /**
     *点对点工具类
     * @param connectionFactory 参数是上面的bean
     * @return
     */
    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerQueue
    (ActiveMQConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory bean=new
                DefaultJmsListenerContainerFactory();
        //设置为是这个工厂
        bean.setConnectionFactory(connectionFactory);
        return bean;
    }

    /**
     * 主题工具类
     * @param connectionFactory 参数是上面的bean
     * @return
     */
    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerTopic
    (ActiveMQConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory bean=new
                DefaultJmsListenerContainerFactory();
        //设置 发部订阅  方式，默认 情况下使用的  生产消费  方式
        bean.setPubSubDomain(true);
        //设置为是这个工厂
        bean.setConnectionFactory(connectionFactory);
        return bean;
    }


}
